package com.appbee.user.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.editors.LabelTextbox;
import com.appbee.user.client.presenter.UserListActivity;
import com.appbee.user.client.text.UserStrings;
import com.appbee.user.shared.proxy.MemberProxy;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.RequestFactory;

public class MemberEditor extends Composite implements Editor<MemberProxy>,UserListActivity.EditMemberView
{

	private static MemberEditorUiBinder uiBinder = GWT
			.create(MemberEditorUiBinder.class);

	interface MemberEditorUiBinder extends UiBinder<Widget, MemberEditor>
	{
	}

	interface Driver extends RequestFactoryEditorDriver<MemberProxy, MemberEditor>
	{
		
	}
	
	@UiField(provided = true)
	IUserlistStyle style;

	@UiField
	LabelTextbox name;
	
	@UiField
	LabelTextbox email;

	@UiField
	LabelTextbox role;

	@UiField
	LabelTextbox imageUrl;

	@UiField
	LabelTextbox description;
	
	@UiField
	Button saveButton;
	
	@UiField
	Button cancelButton;
	
	@UiField(provided = true)
	UserStrings userStrings;

	private UserListActivity presenter;
	
	private final PopupPanel popupPanel = new PopupPanel(true,false);

	@Inject
	public MemberEditor(IUserlistStyle style, UserStrings userStrings)
	{
		this.style = style;
		this.userStrings = userStrings;
		initWidget(uiBinder.createAndBindUi(this));
		popupPanel.setWidget(this);
		cancelButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				popupPanel.hide();
			}
		});
		
		saveButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				presenter.onSave();
			}
		});
	}

	
	@Override
	public RequestFactoryEditorDriver<MemberProxy, MemberEditor> createDriver(
			EventBus eventBus, RequestFactory rf)
	{
		Driver driver = GWT.create(Driver.class);
		driver.initialize(eventBus, rf, this);
		return driver;
	}


	@Override
	public void setPresenter(UserListActivity presenter)
	{
		this.presenter = presenter;
	}



	@Override
	public void show(UIObject button)
	{
		popupPanel.showRelativeTo(button);
		
	}


	@Override
	public void show(int left, int top)
	{
		popupPanel.setPopupPosition(left, top-this.getOffsetHeight());
		popupPanel.show();
	}


	@Override
	public void hide()
	{
		popupPanel.hide();
		
	}

}
